package code07_面向对象;

/**
 * @author 黄衡熙
 * @version 1.0
 * @date 2025年07月21日13:53
 */
public class Test06_构造方法 {
    public static void main(String[] args) {
//        User user = new User();
//        user.name = "xx";
//        user.age = 1;
//        User user2 = new User("admin");
//        System.out.println(user2.name);
//        User user3= new User(18);
        User user4 = new User(18, "jack");
        User user5 = new User(16);
        User user6 = new User("lisi");
    }

}

class User {
    String name;
    int age;


    public User() {
        System.out.println("无参构造方法");
    }

    public User(String a) {
        name = a;
        System.out.println("有参构造方法,方法的参数是name,传入的值为:" + name);
    }

    public User(int age) {
        this.age = age;
        System.out.println("有参构造方法,方法的参数是age,传入的值为:" + age);
    }


    public User(int age, String name) {
//        this.age = age;
        // this(age)相当于手动调用参数为age的构造方法
        this(age);
        // this()调用有且仅能存在一个,且必须处于第一行
//        this(name);
        this.name = name;
        System.out.println("有参构造方法,参数有两个");
    }
}
